package adapters

import (
	"gitee.com/go-caipu-team/go-caipu-plugin-sdk/backend"

	datasource "go-caipu/pkg/services/datasources"
)

// pkg/api/plugin/datasource/admin_datasource.go
//数据源格式适配

// ModelToInstanceSettings converts a datasources.DataSource to a backend.DataSourceInstanceSettings.
func ModelToInstanceSettings(ds *datasource.Datasource) (*backend.DataSourceInstanceSettings, error) {

	//var jsonDataBytes json.RawMessage
	//var err error
	//if ds.JsonData != nil {
	//
	//	jsonDataBytes, err = json.Marshal(&ds.JsonData)
	//	if err != nil {
	//		return nil, fmt.Errorf("failed to convert data source to instance settings: %w", err)
	//	}
	//}
	//type PluginSettings struct {
	//	User string `json:"sysuser"`
	//}
	//settings := PluginSettings{}
	//
	//err = json.Unmarshal([]byte(ds.JsonData), &settings)
	//if err != nil {
	//	return nil, fmt.Errorf("could not unmarshal PluginSettings json: %s", jsonDataBytes)
	//}

	return &backend.DataSourceInstanceSettings{
		ID:       ds.Id,
		Type:     ds.Type,
		Name:     ds.Name,
		URL:      ds.Url,
		JSONData: ds.JsonData,
		Updated:  ds.UpdatedAt,
	}, nil
}
